package com.gxa.jjty.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.mapper.BillMapper;
import com.gxa.jjty.admin.service.BillService;
import com.gxa.jjty.core.pojo.Bill;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Throwable.class)
public class BillServiceImpl extends BaseServiceImpl<Bill> implements BillService {

    @Autowired
    BillMapper billMapper;


    @Override
    public  List selectDayBalance( String startTime, String endTime) throws ParseException {
        List list = null;
        if (startTime  != null && endTime !=null){
            list = traverseSelect(startTime, endTime);
        }else {
            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
            List<Bill> rTimeList = billMapper.selectRiseTime();
            Bill bill = rTimeList.get(0);
            Date sTime = bill.getRiseTime();
            String sTime2 = formatter.format(sTime);
            Bill bill1 = rTimeList.get(rTimeList.size() - 1);
            Date eTime = bill1.getRiseTime();
            String eTime2 = formatter.format(eTime);
            list = traverseSelect(sTime2, eTime2);
        }
        return  list;
    }

    @Override
    public List traverseSelect(String startTime, String endTime) throws ParseException {

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date sTime = formatter.parse(startTime);
        Date eTime = formatter.parse(endTime);
        ArrayList<Object> list = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd ");
        Calendar dd = Calendar.getInstance();

        while (sTime.getTime() <= eTime.getTime()) {
            dd.setTime(sTime);
            Example example = new Example(Bill.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andLike("riseTime", "%" + format.format(sTime) + "%");
            example.orderBy("riseTime").desc();
            List<Bill> bills = billMapper.selectByExample(example);

            if (bills.size() > 0) {
                double balance = bills.get(0).getBalance();
                list.add(balance);
            }
            dd.add(Calendar.DAY_OF_YEAR, 1);
            sTime = dd.getTime();
        }

            return  list;
    }

    @Override
    public List selectInAndOut(String startTime, String endTime) {

        List<Map<String, Object>> list = null;

        list = billMapper.selectInAndOut( startTime, endTime);
        return list;
    }

    @Override
    public Double selectNowBalance() {
        Example example = new Example(Bill.class);
        example.orderBy("riseTime").desc();
        List<Bill> blist = billMapper.selectByExample(example);
        Bill bill = blist.get(0);
        double balance = bill.getBalance();
        System.out.println(balance);

        return balance;
    }

    @Override
    public Double selectTotalRevenue() {
        Double totalRevenue = billMapper.selectTotalRevenue();
        return totalRevenue;
    }


}